/*
 * 项目名称:platform-plus
 * 类名称:CustRecordController.java
 * 包名称:com.platform.modules.cust.controller
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-10-10 14:34:32        klwu     初版做成
 *
 * Copyright (c) 2019-2019 klwu
 */
package com.platform.modules.custmoer.controler;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.RestResponse;
import com.platform.modules.custmoer.entity.CustRecordEntity;
import com.platform.modules.custmoer.entity.StaffEntity;
import com.platform.modules.custmoer.service.CustRecordService;
import com.platform.modules.custmoer.service.CustomerService;
import com.platform.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author klwu
 * @date 2020-10-10 14:34:32
 */
@RestController
@RequestMapping("cust/record")
public class CustRecordController extends AbstractController {
    @Autowired
    private CustRecordService custRecordService;
    @Autowired
    private CustomerService customerService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("cust:record:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<CustRecordEntity> list = custRecordService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("cust:record:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = custRecordService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("cust:record:info")
    public RestResponse info(@PathVariable("id") String id) {
        CustRecordEntity custRecord = custRecordService.getById(id);

        return RestResponse.success().put("record", custRecord);
    }

    /**
     * 新增
     *
     * @param custRecord custRecord
     * @return RestResponse
     */
    @SysLog("新增")
    @RequestMapping("/save")
    @RequiresPermissions("cust:record:save")
    public RestResponse save(@RequestBody CustRecordEntity custRecord) {

        custRecordService.add(custRecord);

        return RestResponse.success();
    }

    /**
     * 修改
     *
     * @param custRecord custRecord
     * @return RestResponse
     */
    @SysLog("修改")
    @RequestMapping("/update")
    @RequiresPermissions("cust:record:update")
    public RestResponse update(@RequestBody CustRecordEntity custRecord) {

        custRecordService.update(custRecord);

        return RestResponse.success();
    }

    /**
     * 根据主键删除
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除")
    @RequestMapping("/delete")
    @RequiresPermissions("cust:record:delete")
    public RestResponse delete(@RequestBody String[] ids) {
        custRecordService.deleteBatch(ids);

        return RestResponse.success();
    }

    @GetMapping("/home/records")
    public RestResponse queryByDays(@RequestParam Map<String, Object> params){
        params.put("diff",7);
        List<String> days = customerService.getLastDays(params);
        List<Integer> nums = new ArrayList();
        List<CustRecordEntity> CustRecordEntitys = custRecordService.queryAll(params);
        int one = 0;
        int two = 0;
        int thrid = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int sev = 0;
        for(CustRecordEntity my : CustRecordEntitys){
            String format = DateUtils.format(my.getCreateTime(), DateUtils.DATE_PATTERN);
            if(format.equals(days.get(0))){
                one ++;
            }
            if(format.equals(days.get(1))){
                two ++;
            }
            if(format.equals(days.get(2))){
                thrid ++;
            }
            if(format.equals(days.get(3))){
                four ++;
            }
            if(format.equals(days.get(4))){
                five ++;
            }
            if(format.equals(days.get(5))){
                six ++;
            }
            if(format.equals(days.get(6))){
                sev ++;
            }
        }
        nums.add(one);
        nums.add(two);
        nums.add(thrid);
        nums.add(four);
        nums.add(five);
        nums.add(six);
        nums.add(sev);
        return RestResponse.success().put("recordnums",nums);
    }
}
